package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
	@Autowired
	private DishService dishService;
	@Autowired
	private DishFlavorService dishFlavorService;
	@Autowired
	private CategoryService categoryService;

	/**
	 * 添加菜品
	 * 一级路径
	 * 参数：json格式，DishDto
	 */
	@PostMapping
	public R<String> save(@RequestBody DishDto dishDto){
		log.info(dishDto.toString());

		dishService.saveDishAndDishFlavor(dishDto);

		return R.success("添加菜品成功");
	}

	/**
	 * 菜品信息分页查询
	 * @param page
	 * @param pageSize
	 * @param name
	 * @return
	 */
	@GetMapping("/page")
	public R<Page<DishDto>> page(Integer page,Integer pageSize,String name){
		//构造分页两个类的分页条件
		Page<Dish> dishPage = new Page<>(page,pageSize);
		Page<DishDto> dishDtoPage = new Page<>(page,pageSize);
		//2.构成DishD查询条件
		LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
		queryWrapper.orderByDesc(Dish::getUpdateTime);
		//3.执行分页条件查询
		dishService.page(dishPage,queryWrapper);//查询后的结果会封装到dishPage
		//4.此时dishPage有数据，但是dishDtoPage没有数据
		BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
		//5.遍历分页查询的records数据
		List<DishDto> dishDtoRecords = dishPage.getRecords().stream().map(dish -> {
			DishDto dishDto = new DishDto();
			BeanUtils.copyProperties(dish,dishDto);
			//根据分类id查询分类信息
			Category category = categoryService.getById(dish.getCategoryId());
			dishDto.setCategoryName(category.getName());
			return dishDto;
		}).collect(Collectors.toList());
		//6.封装数据
		dishDtoPage.setRecords(dishDtoRecords);
		//7.返回分页DishDto的分页数据
		return R.success(dishDtoPage);
	}
	/**
	 * 根菜品修改回显
	 * 请求方法是GET
	 * 返回类型DishDto
	 */
	@GetMapping("/{dish_id}")
	public R<DishDto> getDishDtoById(@PathVariable Long dish_id){
		//1.查询dish
		Dish dish = dishService.getById(dish_id);
		//2.Beanutilscopy属性
		DishDto dishDto = new DishDto();
		BeanUtils.copyProperties(dish,dishDto);
		//3.查询DishFlavor，口味表集合，根据dish_id查询口味表集合
		LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(DishFlavor::getDishId,dish_id);
		List<DishFlavor> list = dishFlavorService.list(queryWrapper);

		//4.口味集合设置到DishDto
		dishDto.setFlavors(list);
		//5.返回
		return R.success(dishDto);

	}
	/**
	 * 修改菜品
	 * @param dishDto
	 * @return
	 */
	@PutMapping
	public R<String> update(@RequestBody DishDto dishDto){
		log.info(dishDto.toString());
		dishService.updateWithFlavor(dishDto);
		return R.success("修改菜品成功");
	}
	/**
	 * 根据菜品id查询菜品集合
	 * 一级路径dish，二级路径list，方法是get
	 * 参数：表单参数，categoryId
	 */
	@GetMapping("/list")
	public R<List<DishDto>> list(Dish dish){
		// LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
		// queryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
		// queryWrapper.eq(Dish::getStatus,1);//在售商品才查询
		// List<Dish> dishes = dishService.list(queryWrapper);
		List<DishDto> dishDtoList = dishService.getDishDtoSByCategoryId(dish);
		return R.success(dishDtoList);
	}

	/**
	 * 根据id修改菜品的状态status(停售和起售)
	 *
	 *0停售，1起售。
	 * @param status
	 * @param
	 * @return
	 */
	@PostMapping("/status/{status}")
	public R<String> updateStatusById(@PathVariable Integer status, Long[] ids) {
		// 增加日志验证是否接收到前端参数。
		log.info("根据id修改菜品的状态:{},id为：{}", status, ids);
		// 通过id查询数据库。修改id为ids数组中的数据的菜品状态status为前端页面提交的status。
		for (int i = 0; i < ids.length; i++) {
			Long id=ids[i];
			//根据id得到每个dish菜品。
			Dish dish = dishService.getById(id);
			dish.setStatus(status);
			dishService.updateById(dish);
		}
		return R.success("修改菜品状态成功");
	}
	/**
	 * 删除菜品
	 * @param ids
	 * @return
	 */
	@DeleteMapping
	public R<String> delete(@RequestParam List<Long> ids){
		log.info("ids:{}",ids);

		dishService.removeWithFlavor(ids);
		return R.success("菜品删除成功");
	}
}



